package com.mengdee.manage.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mengdee.manage.validator.GroupA;
import com.mengdee.manage.validator.GroupAB;
import com.mengdee.manage.validator.GroupB;
import com.mengdee.manage.validator.UserEntity;

@Controller
@RequestMapping("/validated")
public class ValidationController extends BaseController {
	
	// 校验时指定了GroupA，那么只校验约束中包含GroupA的约束，没有包含就不校验，例如对于phone， @NotBlank指定的分组，而@Pattern没有指定分组，那么只校验空着一个约束，不校验手机号格式
	// http://localhost:8081/platform-springmvc-webapp/validated/groupA?password=root123&phone=123
	@RequestMapping(value = "/groupA", method = RequestMethod.GET)
	public @ResponseBody Object groupA(HttpServletRequest request, @Validated({GroupA.class}) UserEntity user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			FieldError fieldError = bindingResult.getFieldError();
			
			return this.ResponseJsonError(fieldError);
		}
		
		return "ok";
	}
	
	// http://localhost:8081/platform-springmvc-webapp/validated/groupB?phone=123&password=root123&id=1
	@RequestMapping(value = "/groupB", method = RequestMethod.GET)
	public @ResponseBody Object groupB(HttpServletRequest request, @Validated({GroupB.class}) UserEntity user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			FieldError fieldError = bindingResult.getFieldError();
			
			return this.ResponseJsonError(fieldError);
		}
		
		return "ok";
	}
	
	// groupAB
	// http://localhost:8081/platform-springmvc-webapp/validated/groupAB?phone=111&password=root123&nickname=123&email=xxx@qq.com
	// @Validated({GroupA.class, GroupB.class}):GroupA和GroupB的关系是或的关系，就像数据库中的OR一样，只要满足一个条件就会对该约束进行校验，同时使用多个组注意多个组之间没有先后属性之说，并不是先校验组A，然后再校验组B
	// 因为id的为空和不为空的约束都会进行检查，所以先注释掉该属性
	@RequestMapping(value = "/groupAB", method = RequestMethod.GET)
	public @ResponseBody Object groupAB(HttpServletRequest request, @Validated({GroupA.class, GroupB.class}) UserEntity user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			FieldError fieldError = bindingResult.getFieldError();
			
			return this.ResponseJsonError(fieldError);
		}
		
		return "ok";
	}

	// default
	// http://localhost:8081/platform-springmvc-webapp/default?email=xxx@163.com&age=18
	// @Validated 如果没有指定groups则验证没有分组的属性（此时和@Valid功能一样）,如果一个字段上有多个约束，都必须没有指定组，如果部分约束指定的组，部分约束没有指定约束，那么在使用@Validated时不进行检查的
	@RequestMapping(value = "/default", method = RequestMethod.GET)
	public @ResponseBody Object defaultGroup(HttpServletRequest request, @Validated UserEntity user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			FieldError fieldError = bindingResult.getFieldError();
			
			return this.ResponseJsonError(fieldError);
		}
		
		return "ok";
	}
	
	//localhost:8081/platform-springmvc-webapp/validated/sequence?phone=123&password=root123&email=123&nickname=123
	// 对于一个属性上有多个约束，并且多个约束不都在同一个组，那么在检查的时候顺序是根据GroupSequence定义的先后顺序来检查的
	@RequestMapping(value = "/sequence", method = RequestMethod.GET)
	public @ResponseBody Object sequence(HttpServletRequest request, @Validated({GroupAB.class}) UserEntity user, BindingResult bindingResult){
		if (bindingResult.hasErrors()) {
			FieldError fieldError = bindingResult.getFieldError();
			
			return this.ResponseJsonError(fieldError);
		}
		
		return "ok";
	}
}
